/**
 * 
 */
package com.snl.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.snl.model.Advertisement;
import com.snl.model.Party;
import com.snl.service.MainService;

/**
 * @author Bryan Liu
 *
 */
public class MainAction extends ActionSupport implements Preparable, ModelDriven<MainAction>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3053869448166998114L;

	private MainService service;

	private List<Party> comingPartyList;
	
	private List<Party> pastPartyList;
	
	private List<Advertisement> adList;
	
	private InputStream iconStream;
	
	private Advertisement ad;
	
	private Integer totalComingEvents;
	
	private Integer totalPastEvents;
	
	private Integer currentFirstComingRecord;
	
	private Integer currentFirstPastRecord;
	
	public MainAction(MainService service) {
		super();
		this.service = service;
	}

	
	public String execute(){
		
		return Action.SUCCESS;
	}
	
	
	public String fetchAdIcon(){
		return Action.SUCCESS;
	}
	
	public InputStream getIconStream(){

		if(ad!=null && ad.getId()!=null){

			try {



				Advertisement adTemp = service.getAdvertisement(ad.getId());

				if(adTemp.getIcon()!=null){
					return new ByteArrayInputStream(adTemp.getIcon());
				}
				
				return null;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return iconStream;
		}else 
			return null;
	}
	
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		comingPartyList = service.listComingPartys(0);
		
		currentFirstComingRecord = 0;
		
		currentFirstPastRecord = 0;
		
		totalComingEvents = service.getTotalComingEventCount();
		
		totalPastEvents = service.getTotalPastEventCount();
		
		pastPartyList = service.listPastPartys(0);
		
		adList = service.listAds();
	}

	

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	@Override
	public MainAction getModel() {
		return null;
	}


	/**
	 * @return the comingPartyList
	 */
	public List<Party> getComingPartyList() {
		return comingPartyList;
	}
	
	

	/**
	 * @param comingPartyList the comingPartyList to set
	 */
	public void setComingPartyList(List<Party> comingPartyList) {
		this.comingPartyList = comingPartyList;
	}

	/**
	 * @return the pastPartyList
	 */
	public List<Party> getPastPartyList() {
		return pastPartyList;
	}

	/**
	 * @param pastPartyList the pastPartyList to set
	 */
	public void setPastPartyList(List<Party> pastPartyList) {
		this.pastPartyList = pastPartyList;
	}

	/**
	 * @return the adList
	 */
	public List<Advertisement> getAdList() {
		return adList;
	}

	/**
	 * @param adList the adList to set
	 */
	public void setAdList(List<Advertisement> adList) {
		this.adList = adList;
	}


	/**
	 * @return the ad
	 */
	public Advertisement getAd() {
		return ad;
	}


	/**
	 * @param ad the ad to set
	 */
	public void setAd(Advertisement ad) {
		this.ad = ad;
	}


	/**
	 * @return the totalComingEvents
	 */
	public Integer getTotalComingEvents() {
		return totalComingEvents;
	}


	/**
	 * @param totalComingEvents the totalComingEvents to set
	 */
	public void setTotalComingEvents(Integer totalComingEvents) {
		this.totalComingEvents = totalComingEvents;
	}


	/**
	 * @return the totalPastEvents
	 */
	public Integer getTotalPastEvents() {
		return totalPastEvents;
	}


	/**
	 * @param totalPastEvents the totalPastEvents to set
	 */
	public void setTotalPastEvents(Integer totalPastEvents) {
		this.totalPastEvents = totalPastEvents;
	}


	/**
	 * @return the currentFirstComingRecord
	 */
	public Integer getCurrentFirstComingRecord() {
		return currentFirstComingRecord;
	}


	/**
	 * @param currentFirstComingRecord the currentFirstComingRecord to set
	 */
	public void setCurrentFirstComingRecord(Integer currentFirstComingRecord) {
		this.currentFirstComingRecord = currentFirstComingRecord;
	}


	/**
	 * @return the currentFirstPastRecord
	 */
	public Integer getCurrentFirstPastRecord() {
		return currentFirstPastRecord;
	}


	/**
	 * @param currentFirstPastRecord the currentFirstPastRecord to set
	 */
	public void setCurrentFirstPastRecord(Integer currentFirstPastRecord) {
		this.currentFirstPastRecord = currentFirstPastRecord;
	}
	
	
	
}
